using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Collections.Generic;
using Pms.Utils;
using Biblioteca.Utils;
using Biblioteca.DataBase;
/*
 * 
 * Autor      : Jose Jornando de Carvalho Junior    
 * Data       : 28/03/2010 10:46:37
 * Descricao  : 
 * Alteracoes : 
 * 
 */

namespace Serix
{
   public class CGruposUsuarios
   {
       private string COD_GRP ;
       private string COD_USU ;

	   /// <sumary>
       /// Construtor da classe grupos_usuarios
       /// <sumary>
       public CGruposUsuarios()
       {
   
       }

	   /// <sumary>
       /// Construtor da classe Grupos_usuarios
       /// <sumary>
       public CGruposUsuarios( string COD_GRP, string COD_USU )
       {
          this.COD_GRP = COD_GRP;
          this.COD_USU = COD_USU;
       }
	   
	   #region Propriedades
     	/// <sumary>
        /// 
        /// <sumary>
        public string Cod_grp 
		{
		   get
		   {
		       return this.COD_GRP;
		   }
		   set
		   {
		       this.COD_GRP = value;
		   }
		}
     	/// <sumary>
        /// 
        /// <sumary>
        public string Cod_usu 
		{
		   get
		   {
		       return this.COD_USU;
		   }
		   set
		   {
		       this.COD_USU = value;
		   }
		}
	    #endregion Propriedades
	
	    /// <summary>
        /// Inserindo um novo Grupos_usuarios.
        /// </summary>
        public void Inserir(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"insert into Serix.SEG_GRUPOS_USUARIOS
			                  ( COD_GRP, COD_USU )
						   values
						      ( :COD_GRP, :COD_USU )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("COD_GRP",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("COD_USU",OracleType.Number)); 
				
                command.Parameters["COD_GRP"].Value = this.COD_GRP;
                command.Parameters["COD_USU"].Value = this.COD_USU;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	    /// <summary>
        /// Alterando um Grupos_usuarios.
        /// </summary>
        public void Alterar(string Login)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"update Serix.SEG_GRUPOS_USUARIOS set 
			                  (  )
						   where
						      ( COD_GRP = :COD_GRP, COD_USU = :COD_USU )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("COD_GRP",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("COD_USU",OracleType.Number)); 
				
                command.Parameters["COD_GRP"].Value = this.COD_GRP;
                command.Parameters["COD_USU"].Value = this.COD_USU;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	     /// <summary>
        /// Exclui um  Grupos_usuarios.
        /// </summary>
        public void Excluir(string Login)
        {
            Tabela tab = new Tabela("SEG_GRUPOS_USUARIOS", BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login,BiblioPMS.Demo));
            tab.adicionarFiltro(new Coluna("COD_GRP", DbType.Int32, this.COD_GRP));
            tab.adicionarFiltro(new Coluna("COD_USU", DbType.Int32, this.COD_USU));
            tab.delete();
        }
		
		static public DataTable ObterTodos()
        {
            string login = "serix";
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SEG_GRUPOS_USUARIOS order by  ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection)); 
                return tbl; 
                
            }
            catch (Exception e)
            {
 
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		
		static public CGruposUsuarios PesquisaporCodigo(string COD_GRP, string COD_USU, string login)
        {
            CGruposUsuarios obj;
            string SQL = @"SELECT *
                            FROM  Serix.SEG_GRUPOS_USUARIOS 
                            WHERE COD_GRP = :COD_GRP, COD_USU = :COD_USU";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.AdicionarParametro(":COD_GRP", DbType.String, COD_GRP);
				qy.AdicionarParametro(":COD_USU", DbType.String, COD_USU);
               
				qy.Executar();
                obj = new CGruposUsuarios( qy.ObterCampo("COD_GRP"), qy.ObterCampo("COD_USU") );
            }                                  
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return new CGruposUsuarios();
            }
			finally
            {
                qy.FecharConexao();
            }
            return obj;
        }
		
		static public int ObterNovoIdt()
        {
		    string login = "serix";
            string SQL = @"SELECT PKG_BIBSERIX.OBTER_IDT_TER() IDT From dual ";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.Executar();
				return Convert.ToInt32(qy.ObterCampo("IDT"));
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return-1;
            }
			finally
            {
                qy.FecharConexao();
            }
        }
    }
}	

